/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.coraci.anag.gui.components.basic.gui;

import it.coraci.anag.db.mongo.entities.Attribute;
import it.coraci.anag.gui.components.basic.gui.logic.CRUDParametrizablePanel;
import it.coraci.anag.gui.components.basic.gui.utils.FileCopyUtil;
import java.awt.Color;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.border.Border;

/**
 *
 * @author Luca
 */
public class SimpleFilePathParamPanel extends javax.swing.JPanel implements CRUDParametrizablePanel,PropertyChangeListener {

    public Attribute attribute;
    private Border ahahBorder = null;
    private AnagDBFileChooserDialog fcd = null;
    private String outputPath = null;

    /**
     * Creates new form ParamPanel
     */
    public SimpleFilePathParamPanel() {
        initComponents();
        this.ahahBorder = this.jTextField_Value.getBorder();
    }

    public SimpleFilePathParamPanel(Attribute attribute,String path) {
        initComponents();
        this.outputPath = path;
        this.ahahBorder = this.jTextField_Value.getBorder();
        this.attribute = attribute;
        this.jLabel1.setText(attribute.getKey());
        if (attribute.getValue() != null) {
            this.jTextField_Value.setText(attribute.getValue());
        }
    }

    public String getOutputPath() {
        return outputPath;
    }

    public void setOutputPath(String outputPath) {
        this.outputPath = outputPath;
    }

    public void setAttribute(Attribute attribute) {
        this.attribute = attribute;
        this.jLabel1.setText(attribute.getKey());
        if (attribute.getValue() != null) {
            this.jTextField_Value.setText(attribute.getValue());
        }
    }

    public Attribute getAttribute() {
        attribute.setKey(this.jLabel1.getText());
        attribute.setValue(this.jTextField_Value.getText());
        return attribute;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField_Value = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("ParamKey:");

        jTextField_Value.setMinimumSize(new java.awt.Dimension(20, 20));

        jButton1.setText("sfoglia");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField_Value, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                .addComponent(jTextField_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        fcd = new AnagDBFileChooserDialog(null, true);
        fcd.addPropertySupportListener(this);
        fcd.setVisible(true);
        fcd.setLocationRelativeTo(this);
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField_Value;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setEditable(boolean editable) {
        this.jTextField_Value.setEditable(editable);
        if(editable){
           this.jTextField_Value.setBorder(ahahBorder);
            this.jTextField_Value.setFont(new Font("Tahoma", Font.BOLD, 12));
            this.jTextField_Value.setForeground(Color.BLACK);
            this.jTextField_Value.setBackground(Color.WHITE);
        }else{
            this.jTextField_Value.setBorder(null);
            this.jTextField_Value.setFont(new Font("Tahoma", Font.BOLD, 12));
            this.jTextField_Value.setForeground(Color.BLUE);
            this.jTextField_Value.setBackground(Color.lightGray);
        }
    }

    @Override
    public boolean isEditable() {
        return this.jTextField_Value.isEnabled();
    }

    @Override
    public Object getValue() {
        return this.getAttribute().getValue();
    }

    @Override
    public void setValue(Object value) {
        if (value != null) {
            this.jTextField_Value.setText(value.toString());
        }
    }

    @Override
    public void clear() {
//        this.attribute = null;
        this.jTextField_Value.setText("");
    }
    
    public void copyFile(){
          if (!jTextField_Value.getText().isEmpty()) {
                String fileName =jTextField_Value.getText();
                try {

//                    String[] split = fileName.split(File.separator);
//                    if(split.length>0){
                        String trueFileName = new File(fileName).getName();
                        
//                    }
                    File auth_dir = new File(".//" + outputPath);
                    System.out.println("DIR: "+".//" + outputPath);
                    System.out.println("FILE NAME : "+trueFileName);
                    if (auth_dir.exists() && auth_dir.isDirectory()) {
                        File client = new File(".//" + outputPath + "//" + trueFileName);
                        if (client.isFile() && client.exists()) {
                            System.out.println("file is already existing ");
                            System.out.println(" trying to delete it");
                            client.delete();
                            System.out.println("client deleted");
                        }
                    } else {
                        System.out.println("directory non existing ..");
                        auth_dir.mkdir();
                        System.out.println("directory created");
                    }
                    FileCopyUtil.copy(fileName, ".//" + outputPath + "//" + trueFileName);
                    System.out.println("copy ok ");
//                    Files.copy(new File(evt.getNewValue().toString()).toPath(),new File(".").toPath(), StandardCopyOption.REPLACE_EXISTING);
                } catch (IOException ex) {
                    ex.printStackTrace();
                } 
            } else {
//                this.jButton_Login.setEnabled(false);
            }
//            }else{
//                this.jTextField_trustStore.setText(evt.getNewValue().toString());
//                this.fcd.setVisible(false);
//                this.fcd.dispose();
//            }
        
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
         if (evt.getPropertyName().equals(JFileChooser.APPROVE_SELECTION)) {
//            if(keyStoreSetting){
            this.jTextField_Value.setText(evt.getNewValue().toString());
            this.fcd.setVisible(false);
            this.fcd.dispose();
            //WRITE IN FILE
//            if (!evt.getNewValue().toString().isEmpty()) {
//                String fileName =evt.getNewValue().toString();
//                try {
//
////                    String[] split = fileName.split(File.separator);
////                    if(split.length>0){
//                        String trueFileName = new File(fileName).getName();
//                        
////                    }
//                    File auth_dir = new File(".//" + outputPath);
//                    System.out.println("DIR: "+".//" + outputPath);
//                    System.out.println("FILE NAME : "+trueFileName);
//                    if (auth_dir.exists() && auth_dir.isDirectory()) {
//                        File client = new File(".//" + outputPath + "//" + trueFileName);
//                        if (client.isFile() && client.exists()) {
//                            System.out.println("file is already existing ");
//                            System.out.println(" trying to delete it");
//                            client.delete();
//                            System.out.println("client deleted");
//                        }
//                    } else {
//                        System.out.println("directory non existing ..");
//                        auth_dir.mkdir();
//                        System.out.println("directory created");
//                    }
//                    FileCopyUtil.copy(evt.getNewValue().toString(), ".//" + outputPath + "//" + trueFileName);
//                    System.out.println("copy ok ");
////                    Files.copy(new File(evt.getNewValue().toString()).toPath(),new File(".").toPath(), StandardCopyOption.REPLACE_EXISTING);
//                } catch (IOException ex) {
//                    ex.printStackTrace();
//                } 
//            } else {
////                this.jButton_Login.setEnabled(false);
//            }
////            }else{
////                this.jTextField_trustStore.setText(evt.getNewValue().toString());
////                this.fcd.setVisible(false);
////                this.fcd.dispose();
////            }
        }
    }
}
